<?php 
require_once '../model/moduleuser.php';
require_once '../model/background_images.php';

$moduleObj = new Model_ModuleUser();
$imageObj  = new Model_Background_Images();

if($_GET['q']=='manageimages') {
	$moduelArray = $moduleObj->getModuleHelpText(10);
	$smarty->assign('moduelArray', $moduelArray);
}


if(isset($_GET['status']) && $_GET['status'] != '') {
	$status  = $_GET['status'];
	$imageId = base64_decode($_GET['id']);
	if($status == '0') {
		$imageObj->updateImageStatus();
		if($activeCount == 0) {
			$newStatus = '1';
		}
		/*$activeCount = $imageObj->getActiveImageCount();
		if($activeCount == 0) {
			$newStatus = '1';
		}
		else {
			$_SESSION['msg'] = "<div class='error_msg'><span>Only one image can be active at a time</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=manageimages');
			exit;
		}*/
	}
	else {
		$newStatus = '0';
	}
	$imageStatusArray = array();
	$imageStatusArray['status'] = $newStatus;
	$imageObj->editBackgroundImage($imageStatusArray,$imageId);
	$_SESSION['msg'] = "<div class='success_msg'><span>Image status changed successfully!!!</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=manageimages');
	exit;
}

if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);	
	$newimageArray = $imageObj->getImageDetails($id);
	$path = '../uploads/logo/'.$newimageArray['image_path'];
	unlink("$path");
	
	## Delete logo
	$imageObj->deleteBackgroundImage($id);
	$_SESSION['msg']="<div class='success_msg'><span>Logo deleted successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=manageimages&page='.$_GET['page']);
	exit;
}
if(isset($_POST['search']) != '' && $_POST['search'] != 'Search') {
	$searchindex = $_POST['search'];
} else if(isset($_GET['search']) &&  $_GET['search']!= '') {
	$searchindex = $_GET['search'];
} else {
	$searchindex = '';
}
$smarty->assign('search', $searchindex);

if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
	$pageNum = $_GET[ 'page' ];
} else {
	$pageNum = 1;
} 
$rowsPerPage = ROW_PER_PAGE;
$pageName    = "home.php?q=manageimages"; 
$images      = $imageObj->getAllImages($searchindex);
$total_rows  = count($images);
$offset	     = ($pageNum - 1) * $rowsPerPage;
$smarty->assign('page', $pageNum);

$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
$smarty->assign('newid', $newid);

$imageArray = $imageObj->getAllImages($searchindex, $rowsPerPage,$offset);
$smarty->assign('imageArray', $imageArray);
//echo "<xmp>"; print_r($imageArray); echo "</xmp>";

if($searchindex != '') {
	$other_id = "search=".$searchindex;
} else {
	$other_id = '';
}

if($total_rows > ROW_PER_PAGE) {
	$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
	$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
	$smarty-> assign("pagination",$pgnation);
}

## Assign success or error msg to smarty variable and unset session variable
if(isset($_SESSION['msg']) && $_SESSION['msg']!= '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}
$smarty->assign('activeclass', 'manageimages');
$smarty->assign('mainmenu', '5');

$smarty->display(TEMPLATEDIR_ADMIN. 'settings/manage_images.tpl');
unset($smarty);
?>